package com.runa.monitor.platinterface.netservice.equipment;

import com.alibaba.fastjson.JSON;
import com.runa.monitor.comm.dto.Archive;
import com.runa.monitor.comm.dto.Node;
import com.runa.persistence.util.SpringContextUtil;
import com.runa.monitor.platinterface.meter3_0.HouseEquIDataService;
import com.runa.monitor.platinterface.meter3_0.task.CtrlValveTask;
import com.runa.monitor.platinterface.meter3_0.task.TaskManager;
import com.runa.monitor.platinterface.backwork.function.TaskFunction;
import com.runa.rccprotocol.dto.equdata.EquData;
import com.runa.rccprotocol.dto.param.ExtendParam;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class ThreePointZeroDriver extends NetEqu<String> {

    public ThreePointZeroDriver(String serial) {
        super(serial, null);
    }

    @Override
    public void readNodeEqu(List<Node> nodes, TaskFunction function, EquData.EquDataType type) {
        HouseEquIDataService dataService = SpringContextUtil.getBean(HouseEquIDataService.class);
        nodes.forEach(node -> dataService.executeMethod(serial(), type, node.getSerial(), function));
    }

    @Override
    public void setNodeEquArchive(List<Archive> archives, TaskFunction function) {
        log.info("琅卡博3.0档案下发---" + JSON.toJSONString(archives));
//        for (Archive archive : archives) {
//            LangKaBoEquRequestData requestData = new LangKaBoEquRequestData(serial());
//            RunaEquArchive meterArchive = new RunaEquArchive();
//            BeanUtils.copyProperties(archive, meterArchive);
//            RccTaskFunction.AllTask taskT = (RccTaskFunction.AllTask) function;
//            CommMeter commMeter = RepositoryService.getCommMeterByNodeSerialForNetEqu(meterArchive.getSerial(), taskT.getSerial());
//            requestData.setNodeEquArchive(meterArchive.getSerial(), commMeter.getFactoryName());
//            String hex = ByteUtil.byteToHexString(requestData.getData(), 0, requestData.getData().length);
//            hex = hexFormat(hex);
////          生成需要下发的hex报文
//            TaskManager taskQueue = SpringContextUtil.getBean(TaskManager.class);
//            SendBufferTask task = new SendBufferTask();
//            task.setNetEquNo(serial());
//            task.setFunction(function);
//            task.setHexBuffer(hex);
//            taskQueue.offer(serial(), task);
//            log.info("琅卡博3.0档案下发---3" + JSON.toJSONString(archives));
//        }
    }

//    public static void main(String[] args) {
//        String s = hexFormat("68590059006841023701000004FF00000201112233440100000000000000001234567140420287111100213612557704004821312D312D310000000000000000000000000000000000000000000000000000008CC32800F49428008CC328009316");
//        System.out.println(s);
//    }

    private String hexFormat(String s) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < s.length(); i += 2) {
            stringBuilder.append(s, i, i + 2).append(" ");
        }
        return stringBuilder.substring(0,stringBuilder.length()-1);
    }

    @Override
    public void clearNodeEquArchive(TaskFunction function) {

    }

    @Override
    public void valveOnOffControl(List<Node> nodeList, boolean ifLock, Integer opening, TaskFunction function) {
        TaskManager taskQueue = SpringContextUtil.getBean(TaskManager.class);
        CtrlValveTask task = new CtrlValveTask();
        task.setNetEquNo(serial());
        task.setEquNo(nodeList.stream().map(node -> {
            function.setStatus(node);
            return node.getSerial();
        }).collect(Collectors.toList()));
        task.setOpening(opening);
        task.setIfLock(ifLock);
        taskQueue.offer(serial(), task);
    }

    @Override
    public void readNodeRecord(List<Integer> indexes, TaskFunction function) {

    }

    @Override
    public void sentPublicUnitRecord(List<Node> nodeList, Integer model, Map param, TaskFunction function) {

    }

    @Override
    public void sentPublicUnitModel(List<Node> nodeList, boolean model, TaskFunction function) {

    }

    @Override
    public void sentExtendParam(ExtendParam param, TaskFunction function) {

    }

    @Override
    public void readPublicUnitRecord(Node valve, Integer model, TaskFunction function) {

    }

    @Override
    public void readPublicUnitAndMeter(Node valve, Node meter, TaskFunction function) {

    }

    @Override
    public void heartbeatUpdate() {

    }

    @Override
    public boolean heartStop() {
        return false;
    }
}
